package com.xgsm.springsecurityauthdemo.domain;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.parser.ParserConfig;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserLogin implements UserDetails {
    private User user;
    //    权限信息
    List<String> permissions;

    //    不让这个属性进行序列化
    @JSONField(serialize = false)
    List<SimpleGrantedAuthority> collect;

    public UserLogin(User user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {//获取权限
        if (collect != null) {
            return collect;
        }
//        流式运算
        collect = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return collect;
    }

    @Override
    public String getPassword() {//获取密码
        return user.getPassword();
    }

    @Override
    public String getUsername() {// 获取用户名
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() { //是否设置过期时间 false是不设置；true是设置
        return true;
    }

    @Override
    public boolean isAccountNonLocked() { //是否没锁  false 没锁  true 有锁
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() { //证书是否没有过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
